package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.dto.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class MiMaAttr  implements AdapterAttr {


    public static void main(String[] args) {
        getGames("2");
    }

    public  static List<InterAttrRes> getGames(String type) {
        String url = "https://api.mimaapp.cn/v5/goods/getGameAttr";
        Map<String, String> params = new HashMap<>();
        params.put("appid","242869747");
        params.put("secret","fHapOYVfXGuntTqZwpMNtlVoOsfXW933");
        params.put("game","wzry");
        String response = HttpUtil.doPost(url, params, null);
        System.out.println(response);
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        Integer status = JSONObject.parseObject(response).getInteger("code");
        if (0 == status) {
            JSONObject jsonObject = JSONObject.parseObject(response);
            String string = JSONObject.parseObject(jsonObject.getString("data")).getString("attrs");
            List<JSONObject> data = JSONObject.parseArray(string, JSONObject.class);
            List<JSONObject> collect = new ArrayList<>();
            if (Objects.equals("1",type)){
                collect = data.stream().filter(d -> d.getString("component").equals("select")).collect(Collectors.toList());
            }else if  (Objects.equals("2",type)){
                collect = data.stream().filter(d -> d.getString("component").equals("select-mult")).collect(Collectors.toList());
            }else if (Objects.equals("3",type)){
                collect = data.stream().filter(d -> d.getString("component").equals("input")).collect(Collectors.toList());
            }
            for (JSONObject object : collect) {
                InterAttrRes interAttrRes = new InterAttrRes();
                String name = object.getString("name");
                String values = object.getString("values");
                interAttrRes.setId(name);
                interAttrRes.setName(name);
                List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                List<String> list = JSONObject.parseArray(values, String.class);
                if(!CollectionUtils.isEmpty(list)) {
                    for (String s : list) {
                        InterAttrRes interAttrRes1 = new InterAttrRes();
                        interAttrRes1.setId(s);
                        interAttrRes1.setName(s);
                        interAttrResList1.add(interAttrRes1);
                    }
                }
                interAttrRes.setInterServerRes(interAttrResList1);
                interAttrResList.add(interAttrRes);
            }
        }
        return interAttrResList;
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String url = "https://api.mimaapp.cn/v5/goods/getGameList";
        Map<String, String> params = new HashMap<>();
        params.put("appid","242869747");
        params.put("secret","fHapOYVfXGuntTqZwpMNtlVoOsfXW933");
        String result = HttpUtil.doPost(url, params, null);
        System.out.println(result);
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("game");
            String title = jsonObject.getString("name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        String url = "https://api.mimaapp.cn/v5/goods/getGameAttr";
        Map<String, String> params = new HashMap<>();
        params.put("appid","242869747");
        params.put("secret","fHapOYVfXGuntTqZwpMNtlVoOsfXW933");
        params.put("game",gameId);
        String response = HttpUtil.doPost(url, params, null);
        System.out.println(response);
        JSONObject jsonObject = JSONObject.parseObject(response);
        String string = JSONObject.parseObject(jsonObject.getString("data")).getString("partitions");
        List<JSONObject> data = JSONObject.parseArray(string, JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            String cate_name = datum.getString("big_partition");
            InterServerRes interServer = new InterServerRes();
            interServer.setId(cate_name);
            interServer.setName(cate_name);
            interServerRes.add(interServer);
            List<InterServerRes> interServerRes1 = new ArrayList<>();
            String servers = datum.getString("small_partition");
            if(StringUtils.isNotEmpty(servers)) {
                List<String> list = JSONObject.parseArray(servers, String.class);
                for (String s : list) {
                    InterServerRes interServer1 = new InterServerRes();
                    interServer1.setId(s);
                    interServer1.setName(s);
                    interServerRes1.add(interServer1);
                    interServer.setInterServerRes(interServerRes1);
                }
            }
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        String url = "https://api.mimaapp.cn/v5/goods/getGameAttr";
        Map<String, String> params = new HashMap<>();
        params.put("appid","242869747");
        params.put("secret","fHapOYVfXGuntTqZwpMNtlVoOsfXW933");
        params.put("game",gameId);
        String response = HttpUtil.doPost(url, params, null);
        System.out.println(response);
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        Integer status = JSONObject.parseObject(response).getInteger("code");
        if (0 == status) {
            JSONObject jsonObject = JSONObject.parseObject(response);
            String string = JSONObject.parseObject(jsonObject.getString("data")).getString("attrs");
            List<JSONObject> data = JSONObject.parseArray(string, JSONObject.class);
            List<JSONObject> collect = new ArrayList<>();
            if (Objects.equals("1",type)){
                collect = data.stream().filter(d -> d.getString("component").equals("select")).collect(Collectors.toList());
            }else if  (Objects.equals("2",type)){
                collect = data.stream().filter(d -> d.getString("component").equals("select-mult")).collect(Collectors.toList());
            }else if (Objects.equals("3",type)){
                collect = data.stream().filter(d -> d.getString("component").equals("input")).collect(Collectors.toList());
            }
            for (JSONObject object : collect) {
                InterAttrRes interAttrRes = new InterAttrRes();
                String name = object.getString("name");
                String values = object.getString("values");
                interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                interAttrRes.setId(name);
                interAttrRes.setName(name);
                List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                List<String> list = JSONObject.parseArray(values, String.class);
                if(!CollectionUtils.isEmpty(list)) {
                    for (String s : list) {
                        InterAttrRes interAttrRes1 = new InterAttrRes();
                        interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                        interAttrRes1.setId(s);
                        interAttrRes1.setName(s);
                        interAttrResList1.add(interAttrRes1);
                    }
                }
                interAttrRes.setInterServerRes(interAttrResList1);
                interAttrResList.add(interAttrRes);
            }
        }
        return interAttrResList;
    }
}